﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using MyHotel_Common;
using System.Data.SqlClient;
namespace MyHotel_DAO
{
    public class NhanVienDAO :DAO
    {
        protected override object createObjectFromDatarow(System.Data.DataRow row)
        {

            NhanVien nv = new NhanVien();
            nv.Diachi = row["Diachi"].ToString();
            nv.Ten = row["Ten"].ToString();
            nv.Tendangnhap = row["TenDangNhap"].ToString();
            nv.Matkhau = row["MatKhau"].ToString();
            nv.Manv = Int32.Parse(row["MaNV"].ToString());
            nv.Ngaysinh = DateTime.Parse(row["NgaySinh"].ToString());
            nv.Sodienthoai = row["SoDienThoai"].ToString();
            nv.IsBlock = Boolean.Parse(row["IsBlock"].ToString());

            return nv;
        }
        public List<NhanVien> getList()
        {
        
            string nameProc = "USP_LAY_DANH_SACH_NV";
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null);

            return getListFromDataTable<NhanVien>(dt); 

           
        }
        public int themNhanvien(NhanVien nv)
        {

            string nameProc = "SP_THEMNHANVIEN";
            SqlParameter[] para = new SqlParameter[7];
            para[0] = new SqlParameter("@Ten",nv.Ten);
            para[1] = new SqlParameter("@NgaySinh",nv.Ngaysinh);
            para[2] = new SqlParameter("@DiaChi",nv.Diachi);
            para[3] = new SqlParameter("@SoDienThoai", nv.Sodienthoai);
            para[4] = new SqlParameter("@MatKhau", nv.Matkhau);
            para[5] = new SqlParameter("@TenDangNhap", nv.Tendangnhap);
            para[6] = new SqlParameter("@IsBlock", nv.IsBlock);

            if (DataProvider.executeStoreProcedureNonQuery(nameProc, para) == 1)
            {
                nameProc = "SP_IDMAX_NHANVIEN";
                DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null);
                int id = int.Parse(dt.Rows[0]["IDMAX"].ToString());

                return id;
            }
            else
                return -1;
        }

        public bool xoaNhanVien(NhanVien nv)
        {            
            string nameProc = "SP_DELETE_NHANVIEN_BY_MANV";
            SqlParameter para = new SqlParameter("@MANV", nv.Manv);
            int result = DataProvider.executeStoreProcedureNonQuery(nameProc, para);
            

            return result == 1;
        }

        public bool kiemtraNhanVien_TonTai_TenDangNhap(string tenDangNhap)
        {
            string name_proc = "USP_KIEMTRANHANVIEN_TONTAI_TENDANGNHAP";
            SqlParameter para = new SqlParameter("@TENDANGNHAP", tenDangNhap);
            DataTable dt = DataProvider.executeStoreProcedureQuery(name_proc, para);
            if (dt.Rows.Count == 1)
            {
                return true;
            }
            return false;
        }

        public bool suaNhanVien(NhanVien nv)
        {
            string nameProc = "USP_UPDATENHANVIEN";
            SqlParameter[] para = new SqlParameter[8];
            para[0] = new SqlParameter("@Ten", nv.Ten);
            para[1] = new SqlParameter("@NgaySinh", nv.Ngaysinh);
            para[2] = new SqlParameter("@DiaChi", nv.Diachi);
            para[3] = new SqlParameter("@SoDienThoai", nv.Sodienthoai);
            para[4] = new SqlParameter("@MatKhau", nv.Matkhau);
            para[5] = new SqlParameter("@TenDangNhap", nv.Tendangnhap);
            para[6] = new SqlParameter("@IsBlock", nv.IsBlock);
            para[7] = new SqlParameter("@manv", nv.Manv);

            return (DataProvider.executeStoreProcedureNonQuery(nameProc, para) == 1);
        }

        public bool resetMatKhau(int maNV)
        {
            string nameProc = "SP_RESET_MATKHAU_NHANVIEN";
            return DataProvider.executeStoreProcedureNonQuery(nameProc, new SqlParameter("@MANV", maNV)) == 1;
        }
    }
}
